﻿//version:1.0.1
var bootstrapWrapper = {
    ver: '1.0',
    modalInstArr:[],
    showDialog: function (id, title, url, callback, params) {
        // 寻找有没有空闲的，如果有就直接用
        var emptyModalInst = null;
        if (this.modalInstArr.length > 0){
            for (var index = 0; index < this.modalInstArr.length; index++) {
                if (this.modalInstArr[index].isShow == false) {
                    emptyModalInst = this.modalInstArr[index];
                    break;
                }
            }
        }
        if (emptyModalInst != null) {
            var modalId = 'modalContainer' + emptyModalInst.id;
            var modalTitleId = 'modalTitle' + emptyModalInst.id;
            var modalFrameId = 'modalFrame' + emptyModalInst.id;
            var modalDlgId = 'modalDialog' + emptyModalInst.id;

            var modalObj = $('#' + modalId);
            modalObj.modal({ backdrop: 'static', keyboard: false });//'show');
            emptyModalInst.isShow = true;

            document.getElementById(modalFrameId).src = url;
            document.getElementById(modalTitleId).innerText = title;
            emptyModalInst.callback = callback;

            if (params) {
                if (params.width) {
                    emptyModalInst.modalDialogDomEl.style.width = (params.width + 60) + 'px';
                    emptyModalInst.frameDomEl.style.width = params.width + 'px';
                    emptyModalInst.autoResize = false;
                } else {
                    emptyModalInst.modalDialogDomEl.style.width = (500 + 60) + 'px';
                    emptyModalInst.frameDomEl.style.width = 500 + 'px';
                    emptyModalInst.autoResize = false;
                }
                if (params.height) {
                    emptyModalInst.modalDialogDomEl.style.height = (params.height + 50) + 'px';
                    emptyModalInst.frameDomEl.style.height = params.height + 'px';
                    emptyModalInst.autoResize = false;
                } else {
                    emptyModalInst.modalDialogDomEl.style.height = (400 + 50) + 'px';
                    emptyModalInst.frameDomEl.style.height = 400 + 'px';
                    emptyModalInst.autoResize = false;
                }
            }
            emptyModalInst.params = params;
            return emptyModalInst;
        } else {
            var modalId = 'modalContainer' + id;
            var modalTitleId = 'modalTitle' + id;
            var modalFrameId = 'modalFrame' + id;
            var modalDlgId = 'modalDialog' + id;
            var htmlTemplate = '<div id="' + modalId + '" class="modal"><div class="modal-dialog" id="' + modalDlgId + '"><div class="modal-content"  style="padding:5px;"><div class="modal-header" style="padding:5px;"><h4 class="blue bigger" id="' + modalTitleId + '">' + title + '</h4></div><div class="modal-body"><iframe id="' + modalFrameId + '" src="' + url + '" frameborder="no" border="0"></iframe></div></div></div></div>';
            var modalEl = document.createElement('div');
            modalEl.id = 'modalWrapper' + id;
            modalEl.innerHTML = htmlTemplate;
            document.body.appendChild(modalEl);

            var modalObj = $('#' + modalId);
            modalObj.modal({ backdrop: 'static', keyboard: false });//'show');

            var modalFrameEl = document.getElementById(modalFrameId);

            var modalInst = {
                id: id,
                modalDialogDomEl: document.getElementById(modalDlgId),
                frameDomEl: modalFrameEl,
                modal: modalObj,
                callback: callback,
                autoResize: true,
                isShow: true,
                params: params
            };

            if (params) {
                if (params.width) {
                    modalInst.modalDialogDomEl.style.width = (params.width + 60) + 'px';
                    modalInst.frameDomEl.style.width = params.width + 'px';
                    modalInst.autoResize = false;
                } else {
                    modalInst.modalDialogDomEl.style.width = (500 + 60) + 'px';
                    modalInst.frameDomEl.style.width = 500 + 'px';
                    modalInst.autoResize = false;
                }
                if (params.height) {
                    modalInst.modalDialogDomEl.style.height = (params.height + 50) + 'px';
                    modalInst.frameDomEl.style.height = params.height + 'px';
                    modalInst.autoResize = false;
                } else {
                    modalInst.modalDialogDomEl.style.height = (400 + 50) + 'px';
                    modalInst.frameDomEl.style.height = 400 + 'px';
                    modalInst.autoResize = false;
                }
            }
            this.modalInstArr.push(modalInst);
            return modalInst;
        }
    },
    closeDialog: function (result, data) {
        modalInst = this.modalInstArr[this.modalInstArr.length - 1];
        this.modalInstArr.splice(this.modalInstArr.length - 1, 1);
        var modalId = 'modalContainer' + modalInst.id;
        var modalFrameId = 'modalFrame' + modalInst.id;
        var wrapperId = 'modalWrapper' + modalInst.id;

        var modalObj = $('#' + modalId);
        modalObj.modal('hide');

        //var modalEl = document.getElementById(wrapperId);
        //modalEl.parentElement.removeChild(modalEl);

        modalInst.isShow = false;
        modalInst.callback(result, data, modalInst.params);
    },
    confirm: function (title, message, callback) {
        bootbox.confirm({
            buttons: {
                confirm: {
                    label: '确认',
                    className: 'btn btn-sm btn-info'
                },
                cancel: {
                    label: '取消',
                    className: 'btn btn-sm'
                }
            },
            message: message,
            callback: callback,
            title: title
        });
    },
    alert: function (title, message, callback) {
        // debugger;
        bootbox.alert({
            buttons: {
                ok: {
                    label: '确认',
                    className: 'btn btn-sm btn-info'
                }
            },
            message: message,
            callback: callback,
            title: title
        });
    },
    growl: function (message) {
        //$.bootstrapGrowl(message, { type: 'success' });

        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-info'
        });
    },
    growlInfo: function (message) {
        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-info'
        });
    },
    growlWarning: function (message) {
        $.gritter.add({
            title: '消息',
            text: message,
            class_name: 'gritter-warning'
        });
    },
    growlError: function (message) {
        //$.bootstrapGrowl(message, { type: 'danger' });

        $.gritter.add({
            title: '错误',
            text: message,
            class_name: 'gritter-error'
        });
    }
};

var cppei = {
    _isBusy: false,
    frameDomEl: null,
    lastSwitchUrl:null,
    frameDomElArr: [],
    modalStack: [],
    modalSeed: 0,
    install: function (iframeId) {
        var frameDomEl = document.getElementById(iframeId);
        if (frameDomEl){
            cppei.frameDomEl = frameDomEl;
            var indexOfSharp = window.location.href.indexOf('#');
            if (indexOfSharp > 0) {
               var viewUrl = window.location.href.substring(indexOfSharp + 1, window.location.href.length);
               cppei.frameDomEl.src = viewUrl;
               window.location.href = window.location.href.substring(0, indexOfSharp + 1);
            }
            setInterval("cppei_listenTick()", 1000)
        }
        window.cppei_frame = this;
    },
    showDialog: function (title, url, callback, params) {
        var modalInst = bootstrapWrapper.showDialog(this.modalSeed, title, url, callback, params);
        this.modalStack.push(modalInst);
        this.modalSeed++;
        return modalInst;
    },
    closeDialog: function (modalInst, result, data) {
        bootstrapWrapper.closeDialog(modalInst, result, data);
        this.modalStack.pop();
    },
    confirm: function (title, message, callback) {
        bootstrapWrapper.confirm(title, message, callback);
    },
    alert: function (title, message, callback) {
        bootstrapWrapper.alert(title, message, callback);
    },
    growl: function (message) {
        bootstrapWrapper.growl(message);
    },
    growlWarning: function (message) {
        bootstrapWrapper.growlWarning(message);
    },
    growlInfo: function (message) {
        bootstrapWrapper.growlInfo(message);
    },
    growlError: function (message) {
        bootstrapWrapper.growlError(message);
    },
    api: {
        getUrl: function (sno, act) {
            return '/webapi2.ashx?sys:mod=cms&sys:sno=' + sno + '&sys:act=' + act;
        },
        getUrlWithTimespan: function (sno, act) {
            return '/webapi2.ashx?sys:mod=cms&sys:sno=' + sno + '&sys:act=' + act + '&_t=' + new Date().getTime();
        }
    },
    switchView: function (url, parentUrl) {
        if (url == '#' || url == '/#' || url == '.#') {
            return false;
        }
        if (this.lastSwitchUrl != url) {
            this.frameDomEl.src = xweb.baseUrl + url;
            this.lastSwitchUrl = url;
            return true;
        }
        return false;
    },
    switchSubView: function (url, parentUrl) {
        if (url == '#' || url == '/#' || url == '.#') {
            return false;
        }
        //var newFrame = $('<iframe></iframe>');
        
        if (this.lastSwitchUrl != url) {
            this.frameDomEl.src = url;
            this.lastSwitchUrl = url;
            return true;
        }
        return false;
    },
    log: function (message) {
        if (console) {
            console.log(message);
        }
    },
    busy: function (isBusy, message) {
        this.busyPartial(isBusy, message);
    },
    busyPartial: function (isBusy, message) {
        if (isBusy) {
            if (this._isBusy == false) {
                if (message) {
                    $.blockUI({ message: "<label style='border:1px solid gray; background:#FFF; padding:5px;'>" + message + "</label>", css: { border: '0px', background: 'none' } });
                } else {
                    $.blockUI({ message: "<label style='border:1px solid gray; background:#FFF; padding:5px;'>处理中，请稍候...</label>", css: { border: '0px', background: 'none' } });
                }
                this._isBusy = true;
            }
        } else {
            if (this._isBusy == true) {
                $.unblockUI();
                this._isBusy = false;
            }
        }
    }
};

cppei_listenTick = function () {
    for (var index = 0; index < cppei.modalStack.length; index++) {
        if (cppei.modalStack[index].autoResize) {
            var frameDomEl = cppei.modalStack[index].frameDomEl;
            var modalDialogDomEL = cppei.modalStack[index].modalDialogDomEl;
            if (frameDomEl && frameDomEl.contentDocument && frameDomEl.contentDocument.body) {
                var bodyHeight = frameDomEl.contentWindow.document.body.scrollHeight;
                var docElementHeight = frameDomEl.contentWindow.document.documentElement.scrollHeight;
                var maxHeight = Math.max(bodyHeight, docElementHeight);
                frameDomEl.style.height = maxHeight + 'px';

                var bodyWidth = frameDomEl.contentWindow.document.body.scrollWidth;
                var docElementWidth = frameDomEl.contentWindow.document.documentElement.scrollWidth;
                var maxWidth = Math.max(bodyWidth, docElementWidth);
                frameDomEl.style.width = maxWidth + 'px';
                modalDialogDomEL.style.width = (maxWidth + 50) + 'px';
            }
        }
    }
    if (cppei.frameDomEl.contentDocument && cppei.frameDomEl.contentDocument.body) {
        var bodyHeight = cppei.frameDomEl.contentWindow.document.body.scrollHeight;
        var docElementHeight = cppei.frameDomEl.contentWindow.document.documentElement.scrollHeight;
        var maxHeight = Math.max(bodyHeight, docElementHeight);
        cppei.frameDomEl.style.height = maxHeight + 'px';
    }
}